$(document).ready(function() {
	gebo_amplify_subscribe.buildSidebar();
	gebo_amplify_subscribe.buildNav();
	gebo_amplify_subscribe.buildBreadCrumb();
	gebo_amplify_subscribe.buildTab();
	gebo_amplify_subscribe.buildSelectOption();
	gebo_amplify_subscribe.buildSelectOptionGroup();
	gebo_amplify_subscribe.updateSidebar();
	gebo_amplify_subscribe.updateNav();
	gebo_amplify_subscribe.updateBreadCrumb();
	gebo_amplify_subscribe.cascadingDelete();
	gebo_amplify_subscribe.removeSidebar();
	gebo_amplify_subscribe.removeNav();
	gebo_amplify_subscribe.removeBreadCrumb();
	gebo_amplify_subscribe.insertNav();
	gebo_amplify_subscribe.insertBreadCrumb();
});
gebo_amplify_subscribe = {
	//定义事件
	//build event		
	buildSidebar : function(){
		amplify.subscribe( "buildSidebar", function(data) {
			if(data==null){
				//提示侧边栏信息丢失
			}else{
				var templateFn = doT.template(document.getElementById('sidebar-template').text);
				$('.sidebar').html(templateFn(data));
			}
		});	
	},
	buildNav : function(){
		amplify.subscribe( "buildNav", function(data) {
			if(data==null){
				//提示导航条信息丢失
			}else{
				var templateFn = doT.template(document.getElementById('nav-template').text);
				$('.navbar-inner').html(templateFn(data));
			}
		});		
	},
	buildBreadCrumb : function(){
		amplify.subscribe( "buildBreadCrumb", function(data) {
			//面包路径
			if(data!=null && $('#jCrumbs').length>0){
				var templateFn = doT.template(document.getElementById('breadCrumb-template').text);
				$('#jCrumbs').html(templateFn(data));
			}
		});		
	},
	buildTab : function(){
		amplify.subscribe( "buildTab", function(data) {
			if(data!=null && $('#mainTabs').length>0){
				var templateFn = doT.template(document.getElementById('tab-template').text);
				$('#mainTabs').html(templateFn(data));
			}
		});		
	},
	buildSelectOption : function(){
		amplify.subscribe( "buildSelectOption", function(obj,data) {
			if(data==null || obj==null || obj.length<1){
				//提示选择信息丢失
			}else{
				var templateFn = doT.template(document.getElementById('select-option-template').text);
				obj.html(templateFn(data));
			}
		});		
	},
	buildSelectOptionGroup : function(){
		amplify.subscribe( "buildSelectOptionGroup", function(obj,data) {
			if(data==null || obj==null || obj.length<1){
				//提示选择信息丢失
			}else{
				var templateFn = doT.template(document.getElementById('select-option-group-template').text);
				obj.html(templateFn(data));
			}
		});		
	},
	//update event
	updateSidebar : function(){
		amplify.subscribe( "updateSidebar", function(datas) {
			var stored_userId = amplify.store.sessionStorage( "userId" );
			var stored_Datas = amplify.store.sessionStorage( "sidebarDatas"+stored_userId );
			var DB = TAFFY(stored_Datas)
			for(var index in datas){
				DB({id:datas[index].id}).update(datas[index]);
			}
			var update_Datas = DB().order("id").get();
			amplify.store.sessionStorage( "sidebarDatas"+stored_userId,update_Datas );
			amplify.publish("buildSidebar",update_Datas);
		});		
	},
	updateNav : function(){
		amplify.subscribe( "updateNav", function(datas) {
			var stored_userId = amplify.store.sessionStorage( "userId" );
			var stored_Datas = amplify.store.sessionStorage( "navDatas"+stored_userId );
			var DB = TAFFY(stored_Datas)
			for(var index in datas){
				DB({id:datas[index].id}).update(datas[index]);
			}
			var update_Datas = DB().order("id").get();
			amplify.store.sessionStorage( "navDatas"+stored_userId,update_Datas );
			amplify.publish("buildNav",update_Datas);
		});		
	},
	updateBreadCrumb : function(){
		amplify.subscribe( "updateBreadCrumb", function(datas) {
			var stored_userId = amplify.store.sessionStorage( "userId" );
			var stored_Datas = amplify.store.sessionStorage( "breadCrumbDatas"+stored_userId );
			var DB = TAFFY(stored_Datas)
			for(var index in datas){
				DB({id:datas[index].id}).update(datas[index]);
			}
			var update_Datas = DB().order("id").get();
			amplify.store.sessionStorage( "breadCrumbDatas"+stored_userId,update_Datas );
			amplify.publish("buildBreadCrumb",update_Datas);
		});		
	},
	//remove event
	cascadingDelete : function(){
		amplify.subscribe( "cascadingDelete", function(DB,data) {
			var count = DB({pid:data.id}).count();
			if(count>0){
				var results = DB({pid:data.id}).get();
				for(var index in results){
					amplify.publish("cascadingDelete",DB,results[index]);
				}
			}
			DB({id:data.id}).remove();
		});		
	},
	removeSidebar : function(){
		amplify.subscribe( "removeSidebar", function(datas) {
			var stored_userId = amplify.store.sessionStorage( "userId" );
			var stored_Datas = amplify.store.sessionStorage( "sidebarDatas"+stored_userId );
			var DB = TAFFY(stored_Datas);
			for(var index in datas){
				amplify.publish("cascadingDelete",DB,datas[index]);
			}
			var update_Datas = DB().order("id").get();
			amplify.store.sessionStorage( "sidebarDatas"+stored_userId,update_Datas );
			amplify.publish("buildSidebar",update_Datas);
		});		
	},
	removeNav : function(){
		amplify.subscribe( "removeNav", function(datas) {
			var stored_userId = amplify.store.sessionStorage( "userId" );
			var stored_Datas = amplify.store.sessionStorage( "navDatas"+stored_userId );
			var DB = TAFFY(stored_Datas);
			for(var index in datas){
				amplify.publish("cascadingDelete",DB,datas[index]);
			}
			var update_Datas = DB().order("id").get();
			amplify.store.sessionStorage( "navDatas"+stored_userId,update_Datas );
			amplify.publish("buildNav",update_Datas);
		});		
	},
	removeBreadCrumb : function(){
		amplify.subscribe( "removeBreadCrumb", function(datas) {
			var stored_userId = amplify.store.sessionStorage( "userId" );
			var stored_Datas = amplify.store.sessionStorage( "breadCrumbDatas"+stored_userId );
			var DB = TAFFY(stored_Datas)
			for(var index in datas){
				DB({id:datas[index].id}).remove();
			}
			var update_Datas = DB().order("id").get();
			amplify.store.sessionStorage( "breadCrumbDatas"+stored_userId,update_Datas );
			amplify.publish("buildBreadCrumb",update_Datas);
		});		
	},
	//insert event
	insertNav : function(){
		amplify.subscribe( "insertNav", function(datas) {
			var stored_userId = amplify.store.sessionStorage( "userId" );
			var stored_Datas = amplify.store.sessionStorage( "navDatas"+stored_userId );
			var DB = TAFFY(stored_Datas);
			for(var index in datas){
				DB.insert(datas[index]);
			}
			var update_Datas = DB().order("id").get();
			amplify.store.sessionStorage( "navDatas"+stored_userId,update_Datas );
			amplify.publish("buildNav",update_Datas);
		});		
	},
	insertBreadCrumb : function(){
		amplify.subscribe( "insertBreadCrumb", function(datas) {
			var stored_userId = amplify.store.sessionStorage( "userId" );
			var stored_Datas = amplify.store.sessionStorage( "breadCrumbDatas"+stored_userId );
			var DB = TAFFY(stored_Datas)
			for(var index in datas){
				DB.insert(datas[index]);
			}
			var update_Datas = DB().order("id").get();
			amplify.store.sessionStorage( "breadCrumbDatas"+stored_userId,update_Datas );
			amplify.publish("buildBreadCrumb",update_Datas);
		});		
	}
	
}
/*test:
var update_SideBarData = [{id:1,text:'update1',pid:0}];
var remove_SideBarData = [{id:10,text:'Section4',class:'icon-cog',pid:0}];
amplify.publish("updateSidebar",update_SideBarData);
amplify.publish("removeSidebar",remove_SideBarData);

var update_NavData = [{"id":1,text:'hjl',location:'right',class:'icon-user',pid:0}];
var remove_NavData = [{"id":1,text:'Username',location:'right',class:'icon-user',pid:0}];
var insert_NavData = [{"id":13,text:'hello',location:'right',class:'icon-user',pid:0}];
amplify.publish("updateNav",update_NavData);
amplify.publish("removeNav",remove_NavData);
amplify.publish("insertNav",insert_NavData);

var update_BreadCrumbData = [{id:6,text:'this is new'}];
var remove_BreadCrumbData = update_BreadCrumbData;
var insert_BreadCrumbData = [{id:7,text:'Hot'}];
amplify.publish("updateBreadCrumb",update_BreadCrumbData);
amplify.publish("removeBreadCrumb",remove_BreadCrumbData);
amplify.publish("insertBreadCrumb",remove_BreadCrumbData);
*/